package com.karst.essentials.chapter03
{
	import flash.events.Event;
	
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Cylinder;
	import org.papervision3d.view.BasicView;
	
	internal class NestingExample extends BasicView
	{
		public function NestingExample()
		{
			super() ;
			createView() ;
			startRendering() ;
		}

		private var redMaterial : ColorMaterial = null ;
		private var blueMaterial : ColorMaterial = null ;
		private var greenMaterial : ColorMaterial = null ;
		private var materials : MaterialsList = null ;
		private var cube : Cube = null ;
		private var cylinder : Cylinder = null ;
		private function createView() : void
		{
			if( redMaterial == null )
				redMaterial = new ColorMaterial( 0xFF0000, .5 ) ;
			if( blueMaterial == null )
				blueMaterial = new ColorMaterial( 0x0000FF, .5 ) ;
			if( greenMaterial == null )
				greenMaterial = new ColorMaterial( 0x00FF00, .5 ) ;
			
			if( materials == null )
			{
				materials = new MaterialsList() ;
				materials.addMaterial( redMaterial, "front" ) ;
				materials.addMaterial( redMaterial, "back" ) ;
				materials.addMaterial( blueMaterial, "top" ) ;
				materials.addMaterial( blueMaterial, "bottom" ) ;
				materials.addMaterial( greenMaterial, "left" ) ;
				materials.addMaterial( greenMaterial, "right" ) ;
			}
			
			if( cube == null )
				cube = new Cube( materials, 80, 400, 300, 4, 4, 4 ) ;
			
			if( cylinder == null )
			{
				cylinder = new Cylinder( null, 80, 300, 8, 8, -1, false, true ) ;
				cylinder.localRotationZ = 90 ;
			}
			
			scene.addChild( cube ) ;
			cube.addChild( cylinder ) ;
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick() ;
			if( cube )
			{
				cube.localRotationX++ ;
				cube.localRotationY++ ;
				cube.localRotationZ += 2 ;
			}
		}
	}
}